package net.maku.module.util;

import jakarta.servlet.http.HttpServletRequest;
import net.maku.module.entity.AgFriendLink;
import net.maku.module.entity.AgHomeConfiguration;
import net.maku.module.entity.AgThirdParty;
import net.maku.module.vo.AgCategoryVO;
import net.maku.module.vo.AgContentCombined;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class UrlReplaceUtil {
    
    private final UrlReplaceConfig urlReplaceConfig;
    
    @Autowired
    public UrlReplaceUtil(UrlReplaceConfig urlReplaceConfig) {
        this.urlReplaceConfig = urlReplaceConfig;
    }
    
    /**
     * 替换URL中的域名
     */
    public String replaceUrl(String originalUrl) {
        if (originalUrl == null || originalUrl.isEmpty()) {
            return originalUrl;
        }
        
        String resultUrl = originalUrl;
        List<String> sourceUrls = urlReplaceConfig.getSourceUrls();
        String targetUrl = urlReplaceConfig.getTargetUrl();
        
        if (sourceUrls == null || targetUrl == null) {
            return originalUrl;
        }
        
        // 遍历所有需要替换的源URL
        for (String sourceUrl : sourceUrls) {
            if (resultUrl.contains(sourceUrl)) {
                resultUrl = resultUrl.replaceAll(sourceUrl, targetUrl);

                // 只处理当前匹配的sourceUrl对应的地址后面的反斜杠
                // 使用正则表达式匹配替换后的targetUrl后面跟着三个反斜杠的情况
               /// String pattern = Pattern.quote(resultUrl) + "\\\\\\\\\\\\\\\\\\\\\\\\\"";
                //String replacement = Matcher.quoteReplacement(resultUrl) + "\\\\\\\\\"";
                ///resultUrl = resultUrl.replaceAll(pattern, replacement);
                //break; // 找到一个匹配的就替换并退出
            }
        }
        
        return resultUrl;
    }
    
    /**
     * 批量替换对象中的URL字段
     */
    public <T> void replaceUrlsInList(List<T> objectList, Function<T, String> urlGetter, BiConsumer<T, String> urlSetter) {
        if (objectList == null || objectList.isEmpty()) {
            return;
        }
        
        for (T obj : objectList) {
            String originalUrl = urlGetter.apply(obj);
            if (originalUrl != null) {
                String newUrl = replaceUrl(originalUrl);
                urlSetter.accept(obj, newUrl);
            }
        }
    }


    /**
     * 获取当前请求的域名
     */
    public static String getRequestDomain(HttpServletRequest request) {
        //HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }

        String serverName = request.getServerName();
        return serverName.toLowerCase();
    }

    /**
     * 判断是否是ansteel.cn域名
     */
    public static boolean isAnsteelDomain(HttpServletRequest request) {
        String domain = getRequestDomain(request);
        System.out.println("请求地址："+domain);
        return domain != null && domain.endsWith("ansteel.cn");
    }
    
    /**
     * 专门为AgCategoryVO列表设计的替换方法
     */
    public void replaceImageUrls(HttpServletRequest request,List<AgCategoryVO> categoryList) {
        if (!isAnsteelDomain(request)) {
            return;
        }
        replaceUrlsInList(
            categoryList,
            AgCategoryVO::getImageUrl,
            AgCategoryVO::setImageUrl
        );
    }

    /**
     * 专门为AgCategoryVO列表设计的替换方法
     */
    public void replaceImageUrlsContent(HttpServletRequest request,List<AgContentCombined> categoryList) {
        if (!isAnsteelDomain(request)) {
            return;
        }
        replaceUrlsInList(
                categoryList,
                AgContentCombined::getImageUrl,
                AgContentCombined::setImageUrl
        );
    }

    public void replaceContent(HttpServletRequest request,List<AgContentCombined> categoryList) {
        if (!isAnsteelDomain(request)) {
            return;
        }
        replaceUrlsInList(
                categoryList,
                AgContentCombined::getContent,
                AgContentCombined::setContent
        );
    }
    public void replaceHref(HttpServletRequest request,List<AgContentCombined> categoryList) {
        if (!isAnsteelDomain(request)) {
            return;
        }
        replaceUrlsInList(
                categoryList,
                AgContentCombined::getHref,
                AgContentCombined::setHref
        );
    }

    public void homeConfig(HttpServletRequest request,List<AgHomeConfiguration> categoryList) {
        if (!isAnsteelDomain(request)) {
            return;
        }
        replaceUrlsInList(
                categoryList,
                AgHomeConfiguration::getJsonContent,
                AgHomeConfiguration::setJsonContent
        );
    }

    public void thirdParty(HttpServletRequest request,List<AgThirdParty> categoryList) {
        if (!isAnsteelDomain(request)) {
            return;
        }
        replaceUrlsInList(
                categoryList,
                AgThirdParty::getQrCode,
                AgThirdParty::setQrCode
        );
    }

    public void sortIsOne(HttpServletRequest request,List<AgThirdParty> categoryList) {
        if (!isAnsteelDomain(request)) {
            return;
        }
        replaceUrlsInList(
                categoryList,
                AgThirdParty::getQrCode,
                AgThirdParty::setQrCode
        );
    }
    public void friendLink(HttpServletRequest request,List<AgFriendLink> categoryList) {
        if (!isAnsteelDomain(request)) {
            return;
        }
        replaceUrlsInList(
                categoryList,
                AgFriendLink::getImageUrl,
                AgFriendLink::setImageUrl
        );
    }
}